using System.Collections.Generic;

namespace IntetfaceEditor4PDA
{
    public class PanelItem
    {
        public int IDPanel = -1;
        public int IDParent = -1;
        public int Type = 1;
        public string Caption = "Control";
        public int Length = 10;
        public int Lines = 1;
        public string FieldName = "";
        public string IdQuery = "";
        public string trueVal = "1";
        public string falseVal = "0";
        public string IdTrue = "";
        public string IdFalse = "";
        public string valType = "0";
        public string minVal = "0";
        public string maxVal = "0";

        public static PanelItem ReadPanel(int IDPanel)
        {
            PanelItem item = new PanelItem();
            QuerySelect query = new QuerySelect();
            query.Select(string.Format(SQLControlsReader.SelectInterfaceByID, IDPanel));
            DataRows row = query.GetRows()[0];
            item.IDPanel = int.Parse(row.FieldByName("IDCONTROLINTERFACE"));
            item.IDParent = int.Parse(row.FieldByNameDef("IDOWNCONTROL", "-1"));
            item.Type = int.Parse(row.FieldByName("IDCONTROLTYPE"));
            item.FieldName = row.FieldByNameDef("FIELDNAME", "");
            item.Caption = row.FieldByName("CONTROLNAME");
            item.Lines = int.Parse(row.FieldByNameDef("CONTROLLINESCOUNT", "0"));
            item.Length = int.Parse(row.FieldByNameDef("CONTROLLENGHT", "0"));
            item.IdQuery = row.FieldByNameDef("IDQRYLIST", "");
            item.IdTrue = row.FieldByNameDef("CHECKTRUE", "");
            item.IdFalse = row.FieldByNameDef("CHECKFALSE", "");
            item.valType = row.FieldByNameDef("valType", "");
            item.minVal = row.FieldByNameDef("minVal", "");
            item.maxVal = row.FieldByNameDef("maxVal", "");
            return item;
        }

        public static void AddItem(PanelItem newitem)
        {
            new QueryExec().Execute(string.Format(SQLControlsReader.InsertNewChildControl, newitem.IDPanel, newitem.IDParent, newitem.Type, newitem.Caption));
        }

        public static void Update(PanelItem item)
        {
            new QueryExec().Execute(string.Format(SQLControlsReader.UpdateControl,
                                                  item.IDPanel,
                                                  item.Type,
                                                  item.FieldName.Replace("'", "''"),
                                                  item.Caption.Replace("'", "''"),
                                                  item.Length,
                                                  item.Lines,
                                                  item.IdTrue.Length == 0 ? "NULL" : item.IdTrue,
                                                  item.IdFalse.Length == 0 ? "NULL" : item.IdFalse,
                                                  item.IdQuery.Length == 0 ? "NULL" : item.IdQuery,
                                                  item.valType,
                                                  item.minVal,
                                                  item.maxVal
                                        ));
            
        }
    }
}